package com.demo.core.service.base.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.demo.core.dao.RoleFunctionDao;
import com.demo.core.model.RoleFunction;
import com.demo.core.service.base.RoleFunctionService;

/**
 * 角色功能服务实现类
 * 
 * @author ryuu.kk
 *
 */
public class RoleFunctionServiceImpl implements RoleFunctionService {

	/**
	 * 角色功能数据访问层
	 */
	private RoleFunctionDao roleFunctionDao;
	
	@Override
	public Long save(RoleFunction roleFunction) {
		return roleFunctionDao.insert(roleFunction);
	}
	
	@Override
	@Transactional()
	public void saveList(Long roleId, List<RoleFunction> roleFunctionList) {
		for (RoleFunction rf : roleFunctionList) {
			rf.setRoleId(roleId);
			rf.setCreateDate(new Date());
			rf.setEnabled(Boolean.TRUE);
			if (rf.getStatus() == null) {
				rf.setStatus("RWD");
			}
			save(rf);
		}
	}
	
	@Override
	public boolean exist(Long id) {
		return roleFunctionDao.exists(id);
	}
	public boolean exists(Long id) {
		return roleFunctionDao.exists(id);
	}
	@Override
	public void delete(Long id) {
		roleFunctionDao.delete(id);
	}
	@Override
	public void deleteAll(Long roleId, Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("roleId", roleId);
		params.put("siteId", siteId);
		roleFunctionDao.delete(params);
	}
	@Override
	public void deleteRole(Long functionId, Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(5);
		params.put("functionId", functionId);
		params.put("siteId", siteId);
		roleFunctionDao.delete(params);
	}
	@Override
	public void update(Long id, boolean enabled, String status) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		RoleFunction rf = new RoleFunction();
		rf.setEnabled(enabled);
		rf.setStatus(status);
		roleFunctionDao.update(rf, params);
	}
	@Override
	public RoleFunction find(Long id) {
		return roleFunctionDao.select(id);
	};
	@Override
	public List<RoleFunction> findAll(Long roleId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("roleId", roleId);
		return roleFunctionDao.select(params);
	}
	
	@Override
	public List<RoleFunction> query(Long roleId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("roleId", roleId);
		return roleFunctionDao.queryFunctionUnion(params);
	}

	@Override
	public List<RoleFunction> query(Long functionId, Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("functionId", functionId);
		params.put("siteId", siteId);
		return roleFunctionDao.queryFunctionUnion(params);
	}
	
	@Override
	public List<RoleFunction> query(Long siteId, Long roleId, Boolean rEnabled) {
		Map<String, Object> params = new HashMap<String, Object>(5);
		params.put("roleId", roleId);
		params.put("siteId", siteId);
		params.put("rEnabled", rEnabled);
		return roleFunctionDao.queryFunctionUnion(params);
	}
	
	@Override
	public Map<Long, RoleFunction> queryMap(Long siteId, Long roleId, Boolean enabled) {
		Map<String, Object> params = new HashMap<String, Object>(5);
		params.put("roleId", roleId);
		params.put("siteId", siteId);
		params.put("rEnabled", enabled);
		
		return roleFunctionDao.queryFunctionMap(params);
	}
	
	public void setRoleFunctionDao(RoleFunctionDao roleFunctionDao) {
		this.roleFunctionDao = roleFunctionDao;
	}
}